package com.huishu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.user.mapper.PopupMapper;
import com.huishu.user.pojo.dto.PopupDTO;
import com.huishu.user.pojo.model.PopupDO;
import com.huishu.user.service.PopupService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-22
 */
@Service
public class PopupServiceImpl extends ServiceImpl<PopupMapper, PopupDO> implements PopupService {

    @Override
    public Result create(PopupDTO popupDTO) {
        PopupDO popupDO = BeanUtils.copyProperties(popupDTO, PopupDO.class);
        boolean save = this.save(popupDO);
        return save ? Result.success() : Result.fail();
    }

    @Override
    public Result modify(PopupDTO popupDTO) {
        // 查询是否有开启的弹窗
        // if (popupDTO.getIsOpen()) {
        //     LambdaQueryWrapper<PopupDO> queryWrapper = Wrappers.lambdaQuery(PopupDO.class)
        //             .eq(PopupDO::getIsOpen, true);
        //     PopupDO one = this.getOne(queryWrapper);
        //     if (one != null && !one.getId().equals(popupDTO.getId())) {
        //         return Result.fail("当前已经存在开启的弹窗,不可存在多个同时开启的弹窗");
        //     }
        // }
        PopupDO popupDO = BeanUtils.copyProperties(popupDTO, PopupDO.class);
        boolean save = this.updateById(popupDO);
        return save ? Result.success() : Result.fail();
    }
}
